package core
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.system.ApplicationDomain;
	import flash.utils.ByteArray;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;

	public class Config
	{
		/**
		 * 地表和楼梯的类型
		 */
		public static const air : int = 0;
		public static const groundType : int = 1;
		public static const ladderUp : int = 2;
		public static const ladderDown : int = 3;
		public static const ladderNormal : int = 4;


		public static var stage : MapEditor;
		public static var swfBytes : Array = [];
		public static var appDomain : ApplicationDomain;

		public static var mapRows : int;
		public static var mapCols : int;
		public static var tileWidth : int;
		public static var tileHeight : int;
		public static var mapID : String;
		public static var resUrl : Array;
		public static var mapBgUrl : String;

		public function Config()
		{
		}

		/**
		 * 根据类名创建出对象
		 * @param name
		 * @return
		 *
		 */
		public static function createComponetByName(name : String) : DisplayObject
		{
			if(!name)
				return null;
			var res_Class : Class = appDomain.getDefinition(name) as Class;

			if(!res_Class)
				return null;
			var child : * = new res_Class();

			if(child is BitmapData)
			{
				return new Bitmap(child);
			}
			return child;
		}


		/**
		 * 只能克隆public属性的对象
		 * 构造函数也不能有参数
		 * @param obj
		 * @return
		 *
		 */
		public static function clone(obj : *) : *
		{
			if(obj == null || obj is int || obj is Number || obj is uint || obj is String || obj is Boolean)
			{
				return obj;
			}
			var data : XML = describeType(obj);
			var className : String = data.@name.toXMLString();
			var cloneClass : Class = getDefinitionByName(className) as Class;
			var cloneObj : * = new cloneClass();

			if(obj is Array || obj is Vector)
			{
				for each(var value : * in obj)
				{
					if(value is int || value is Number || value is uint || value is String || value is Boolean)
					{
						cloneObj.push(value);
					}
					else
					{
						cloneObj.push(clone(value));
					}
				}
			}

			if(data.@name == "Object")
			{
				readObject(cloneObj, obj);
			}
			else
			{
				for each(var variable : * in data.variable)
				{
					var key : String = String(variable.@name);
					cloneObj[key] = obj[key];
				}
			}
			return cloneObj;
		}

		public static function readObject(child : *, obj : Object) : void
		{
			var value : *;
			var key : String;
			var data : XML = describeType(child);

			if(data.@name == "Object")
			{
				for(key in obj)
				{
					child[key] = obj[key];
				}
				return;
			}

			for each(var variable : * in data.variable)
			{
				key = String(variable.@name);
				value = obj[key];

				if(value == null || value == undefined)
				{
					continue;
				}

				if(child[key] is int)
				{
					child[key] = int(value);
				}
				else if(child[key] is Number)
				{
					child[key] = Number(value);
				}
				else if(child[key] is Boolean)
				{
					child[key] = Boolean(int(value));
				}
				else if(child[key] is Array)
				{
					child[key] = clone(value);
				}
				else if(child[key] is GameData)
				{
					readObject(child[key], value);
				}
				else
				{
					child[key] = value;
				}
			}

			if(child is GameData)
			{
				GameData(child).checkData();
			}
		}
	}
}